package vip.xiaonuo.app.core.tx;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.log.Log;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.tencentyun.TLSSigAPIv2;
import vip.xiaonuo.app.modular.im.pojo.AccountImport;
import vip.xiaonuo.app.modular.im.pojo.PortraitSet;
import vip.xiaonuo.core.context.constant.ConstantContextHolder;
import vip.xiaonuo.core.pojo.im.TxImConfigs;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static vip.xiaonuo.app.core.consts.AppConstant.CODE_RETURN_TX;

/**
 * <p>Title : 腾讯ＩＭ</p>
 * <p>Description : </p>
 * <p>DevelopTools : IDEA</p>
 * <p>DevelopSystem : Windows11</p>
 *
 * @Author : zxw
 * @Date 2023/11/10 16:49
 */
public class TxIm {

    private static final Log log = Log.get();

    /**
     * 签名生成
     *
     * @return
     */
    public static String ImSignatureGeneration() {
        TxImConfigs txImConfigs = ConstantContextHolder.getTxImConfigs();
        //腾讯云IM签名创建 所有IM服务端接口都需要签名信息
        TLSSigAPIv2 sigApi = new TLSSigAPIv2(Long.parseLong(txImConfigs.getAppId()), txImConfigs.getKey());
        //有效期1小时：60*60秒
        return sigApi.genSig(txImConfigs.getAdminCount(), 3600);

    }


    /**
     * 添加用户
     *
     * @param userCode
     * @param nick
     * @param pic
     * @return
     */
    public static int accountImport(String userCode, String nick, String pic) {
        TxImConfigs txImConfigs = ConstantContextHolder.getTxImConfigs();
        String sigStr = ImSignatureGeneration();

        String url = "https://console.tim.qq.com/v4/im_open_login_svc/account_import?sdkappid=" + txImConfigs.getAppId() + "&identifier=" + txImConfigs.getAdminCount() + "&usersig=" + sigStr + "&random=99999999&contenttype=json";
        //封装参数
        AccountImport accountImport = new AccountImport();
        accountImport.setIdentifier(userCode + "");
        accountImport.setFaceUrl(pic);
        accountImport.setNick(nick);
        String jsonString = JSON.toJSONString(accountImport);
        //发送请求
        String resultContent = HttpPostUtils.getInstance().sendPostBody(url, "", jsonString);
        JSONObject jsonObject = JSONObject.parseObject(resultContent);
        Object actionStatus = jsonObject.get("ActionStatus");
        Object errorCode = jsonObject.get("ErrorCode");
        Object errorInfo = jsonObject.get("ErrorInfo");
        System.out.println(actionStatus);
        System.out.println(resultContent);
        System.out.println(errorInfo);
        return Integer.parseInt(errorCode.toString());
    }


    public static boolean updateAccountSet(String userCode, String nick, String pic) {
        TxImConfigs txImConfigs = ConstantContextHolder.getTxImConfigs();
        String sigStr = ImSignatureGeneration();
        //封装参数
        if (ObjectUtil.isNotEmpty(nick)) {

            String url = "https://console.tim.qq.com/v4/profile/portrait_set?sdkappid=" + txImConfigs.getAppId() + "&identifier=" + txImConfigs.getAdminCount() + "&usersig=" + sigStr + "&random=99999999&contenttype=json";

            PortraitSet portraitSet = new PortraitSet();
            portraitSet.setFromAccount(userCode);

            List<Object> list = new ArrayList<Object>();
            //昵称
            Map<String, Object> nickMap = new HashMap<>(16);
            nickMap.put("Tag", "Tag_Profile_IM_Nick");
            nickMap.put("Value", nick);
            list.add(nickMap);
            portraitSet.setProfileItem(list);
            String jsonString = JSON.toJSONString(portraitSet);
            //发送请求
            String resultContent = HttpPostUtils.getInstance().sendPostBody(url, "", jsonString);
            JSONObject jsonObject = JSONObject.parseObject(resultContent);
            Object errorCode = jsonObject.get("ErrorCode");
            if (CODE_RETURN_TX.equals(errorCode.toString())) {
                return true;
            } else {
                return false;
            }
        }

        if (ObjectUtil.isNotEmpty(pic)) {

            String url = "https://console.tim.qq.com/v4/profile/portrait_set?sdkappid=" + txImConfigs.getAppId() + "&identifier=" + txImConfigs.getAdminCount() + "&usersig=" + sigStr + "&random=99999999&contenttype=json";

            PortraitSet portraitSet = new PortraitSet();
            portraitSet.setFromAccount(userCode);

            List<Object> list = new ArrayList<Object>();
            //昵称
            Map<String, Object> picMap = new HashMap<>(16);
            picMap.put("Tag", "Tag_Profile_IM_Image");
            picMap.put("Value", pic);
            list.add(picMap);
            portraitSet.setProfileItem(list);
            String jsonString = JSON.toJSONString(portraitSet);
            //发送请求
            String resultContent = HttpPostUtils.getInstance().sendPostBody(url, "", jsonString);
            JSONObject jsonObject = JSONObject.parseObject(resultContent);
            Object errorCode = jsonObject.get("ErrorCode");
            if (CODE_RETURN_TX.equals(errorCode.toString())) {
                return true;
            } else {
                return false;
            }
        }
        return false;
    }


}
